package com.yupi.springbootinit.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.yupi.springbootinit.constant.BiMqConstant;

import java.nio.charset.StandardCharsets;

/**
 * 单向接收生产者（发送给前端）
 * @author 穹龙
 * @date 2025/1/12
 */
public class FrontendSingleProducer {

    public static void main(String[] argv) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        // 建立连接，创建频道
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            String EXCHANGE_NAME = BiMqConstant.BI_FRONTEND_EXCHANGE_NAME;
            String routingKey = BiMqConstant.BI_FRONTEND_ROUTING_KEY + "_" + 1855592333758451713L;
            // 发送消息
            String message = "Hello World!";
            System.out.println("routingKey = " + routingKey);
            channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes(StandardCharsets.UTF_8));

        }
    }
}
